package client_gui;

import java.awt.Component;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import client_gui_chat.StreamingPanel;

/**
 * Implementacija JOptionPane-a.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class MyOptionPane implements WindowListener {

	private JDialog paneDialog;
	private JOptionPane pane;

	
	private MyOptionPane(Component parent, String message, String title, int optionType, int messageType, boolean isModal) {
		super();

		pane = new JOptionPane(message, messageType, optionType);
		paneDialog = pane.createDialog(parent, title);
		paneDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		paneDialog.addWindowListener(this);
		paneDialog.setModal(isModal);
		pane.setValue(JOptionPane.DEFAULT_OPTION);
//		paneDialog.setVisible(true);
	}


	/**
	 * Vraca referencu i prikazuje odgovarajuci JOptionPane.
	 * @param parent
	 * @param message
	 * @param title
	 * @param optionType
	 * @param messageType
	 * @return referenca na prikazani JOptionPane
	 */
	public static MyOptionPane showOptionPane(Component parent, String message, String title, int optionType, int messageType, boolean isModal) {
		return new MyOptionPane(parent, message, title, optionType, messageType, isModal);
	}
	
	
	/**
	 * Vraca vrednost selektovanog dugmeta.
	 * @return vrednost selektovanog dugmeta
	 */
	public int getValue() {
		if ( pane.getValue() == null )
			return JOptionPane.DEFAULT_OPTION;

		return (Integer)pane.getValue();
	}
	
	
	/**
	 * Zatvara JOptionPane.
	 */
	public void closePane() {
//		pane.setVisible(false);
//		paneDialog.setVisible(false);
		pane.setValue(StreamingPanel.NO_ANSWER);
		paneDialog.dispose();
	}
	
	
	/**
	 * Setuje da li je pane vidiljiv.
	 * @param flag - true, ako jeste, false inace
	 */
	public void setVisible(boolean flag) {
		paneDialog.setVisible(flag);
	}
	
	
	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
//		closePane();
	}
	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	
}
